﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.LIB;

namespace HYElectricSystemSetting
{
	public partial class WireSizeForm : System.Windows.Forms.Form
	{
		public WireSizeForm()
		{
			this.InitializeComponent();
			this.btnDeleteItems.Image = Resource.GetImage("delete16");
		}

		private void btnAdditem_Click(object sender, EventArgs e)
		{
			WireSizeForm.wireSizeIsEdit = true;
			new CreateItem(this.cmbWireType, LineType.WireType).ShowDialog();
		}

		private void tsbAddDataRow_Click(object sender, EventArgs e)
		{
		}

		private void toolStripButton2_Click(object sender, EventArgs e)
		{
			this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
		}

		private void WireSizeForm_Load(object sender, EventArgs e)
		{
			ToolTip toolTip = new ToolTip();
			toolTip.AutoPopDelay = 5000;
			toolTip.InitialDelay = 1000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.btnAddItems, "添加");
			toolTip.SetToolTip(this.btnReName, "编辑");
			toolTip.SetToolTip(this.btnDeleteItems, "删除");
			this.AddComboxItem();
			if (this.cmbWireType.Items.Count > 0)
			{
				this.cmbWireType.SelectedIndex = 0;
			}
		}

		public void AddComboxItem()
		{
			this.cmbWireType.Items.Clear();
			if (ElectricSystemSettingData.WireTypeDatas == null)
			{
				return;
			}
			foreach (WireTypeData item in ElectricSystemSettingData.WireTypeDatas)
			{
				this.cmbWireType.Items.Add(item);
				this.cmbWireType.DisplayMember = "WireType";
			}
		}

		private void btnDeleteItem_Click(object sender, EventArgs e)
		{
			if (this.cmbWireType.Items.Count == 1)
			{
				MessageBox.Show("至少有一种导线类型！删除失败", "盈建科提示");
				return;
			}
			if (this.cmbWireType.SelectedItem != null && DialogResult.Yes == MessageBox.Show("是否要将选中项删除？", "盈建科提示", MessageBoxButtons.YesNo))
			{
				WireTypeData wireTypeData = (WireTypeData)this.cmbWireType.SelectedItem;
				foreach (WireTypeData wireTypeData2 in ElectricSystemSettingData.WireTypeDatas)
				{
					if (wireTypeData2.WireType == wireTypeData.WireType)
					{
						ElectricSystemSettingData.WireTypeDatas.Remove(wireTypeData2);
						break;
					}
				}
				foreach (WireAmpacityData wireAmpacityData in ElectricSystemSettingData.WireAmpacity)
				{
					if (wireAmpacityData.WireType == wireTypeData.Id)
					{
						ElectricSystemSettingData.WireAmpacity.Remove(wireAmpacityData);
						break;
					}
				}
				foreach (EnvTemperatureCorrectionFactorData envTemperatureCorrectionFactorData in ElectricSystemSettingData.EnvTemperatureCorrection)
				{
					if (envTemperatureCorrectionFactorData.WireType == wireTypeData.Id)
					{
						ElectricSystemSettingData.EnvTemperatureCorrection.Remove(envTemperatureCorrectionFactorData);
						break;
					}
				}
				this.cmbWireType.Items.RemoveAt(this.cmbWireType.SelectedIndex);
				if (this.cmbWireType.Items.Count > 0)
				{
					this.cmbWireType.SelectedIndex = 0;
				}
				WireSizeForm.wireSizeIsEdit = true;
			}
		}

		private void cmbWireType_SelectedIndexChanged(object sender, EventArgs e)
		{
			WireTypeData wireTypeData = (WireTypeData)this.cmbWireType.SelectedItem;
			int num = 0;
			foreach (WireAmpacityData wireAmpacityData in ElectricSystemSettingData.WireAmpacity)
			{
				if (wireAmpacityData.WireType == wireTypeData.Id)
				{
					this.dataGridView1.Rows.Clear();
					Dictionary<double, double> ampacityItems = wireAmpacityData.AmpacityItems;
					if (ampacityItems.Count != 0)
					{
						this.dataGridView1.Rows.Add(ampacityItems.Count);
						this.dataGridView1.Rows[num].ReadOnly = true;
						int num2 = 0;
						using (Dictionary<double, double>.Enumerator enumerator2 = ampacityItems.GetEnumerator())
						{
							while (enumerator2.MoveNext())
							{
								KeyValuePair<double, double> keyValuePair = enumerator2.Current;
								this.dataGridView1.Rows[num].ReadOnly = true;
								this.dataGridView1.Rows[num2].Cells[0].Value = keyValuePair.Key;
								this.dataGridView1.Rows[num2].Cells[1].Value = keyValuePair.Value;
								num2++;
								num++;
							}
							break;
						}
					}
				}
			}
			this.dataGridView1.Sort(this.dataGridView1.Columns[0], ListSortDirection.Ascending);
		}

		private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
			{
				this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
				this.dataGridView1.BeginEdit(true);
			}
		}

		private void cmbWireType_MouseClick(object sender, MouseEventArgs e)
		{
		}

		public void SaveFormData(DataGridView dgv)
		{
			bool flag = true;
			WireSizeForm.wireSizeIsEdit = true;
			WireTypeData wireTypeData = (WireTypeData)this.cmbWireType.SelectedItem;
			if (wireTypeData == null)
			{
				return;
			}
			foreach (WireAmpacityData wireAmpacityData in ElectricSystemSettingData.WireAmpacity)
			{
				if (wireAmpacityData.WireType == wireTypeData.Id)
				{
					flag = false;
					Dictionary<double, double> dictionary = new Dictionary<double, double>();
					for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
					{
						if (dgv.Rows[i].Cells[0].Value != null && dgv.Rows[i].Cells[0].Value.ToString() != "" && dgv.Rows[i].Cells[1].Value != null)
						{
							dictionary.Add(Convert.ToDouble(dgv.Rows[i].Cells[0].Value), Convert.ToDouble(dgv.Rows[i].Cells[1].Value));
						}
					}
					wireAmpacityData.AmpacityItems = dictionary;
					break;
				}
			}
			if (flag)
			{
				WireAmpacityData wireAmpacityData2 = new WireAmpacityData();
				wireAmpacityData2.WireType = wireTypeData.Id;
				Dictionary<double, double> dictionary2 = new Dictionary<double, double>();
				for (int j = 0; j < this.dataGridView1.Rows.Count; j++)
				{
					if (dgv.Rows[j].Cells[0].Value != null && dgv.Rows[j].Cells[0].Value.ToString() != "")
					{
						dictionary2.Add(Convert.ToDouble(dgv.Rows[j].Cells[0].Value), Convert.ToDouble(dgv.Rows[j].Cells[1].Value));
					}
				}
				wireAmpacityData2.AmpacityItems = dictionary2;
				ElectricSystemSettingData.WireAmpacity.Add(wireAmpacityData2);
			}
		}

		private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			this.SaveFormData(this.dataGridView1);
		}

		private void btnAddItems_Click(object sender, EventArgs e)
		{
			if (ElectricSystemSettingData.DataGridViewAddRow(0, this.dataGridView1))
			{
				this.SaveFormData(this.dataGridView1);
			}
		}

		private void btnDeleteItems_Click(object sender, EventArgs e)
		{
			if (this.dataGridView1.Rows.Count >= 1 && this.dataGridView1.SelectedRows.Count > 0)
			{
				this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
				this.SaveFormData(this.dataGridView1);
			}
		}

		private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
		{
			this.dataGridView1.FirstDisplayedScrollingRowIndex = e.RowIndex;
		}

		private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			this.dgvTxt = (DataGridViewTextBoxEditingControl)e.Control;
			this.dgvTxt.SelectAll();
			this.dgvTxt.KeyPress += this.Cells_KeyPress;
		}

		private void Cells_KeyPress(object sender, KeyPressEventArgs e)
		{
			ElectricSystemSettingData.keyPressXS(e, this.dgvTxt);
		}

		private void btnReName_Click(object sender, EventArgs e)
		{
			if (ElectricSystemSettingData.DatagridviewEditRow(0, this.dataGridView1))
			{
				this.SaveFormData(this.dataGridView1);
			}
		}

		public static bool wireSizeIsEdit;

		public DataGridViewTextBoxEditingControl dgvTxt;
	}
}
